package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
	
	@Autowired
	private DishMapper dishMapper;
	
	@Autowired
	private DishFlavorMapper dishFlavorMapper;
	
	@Autowired
	private SetmealDishMapper setmealDishMapper;
	
	/**
	 * 分页查询
	 *
	 * @param dishPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult getDish(DishPageQueryDTO dishPageQueryDTO) {
		// 插件自动计算分页
		PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
		// 获取返回所有信息
		Page<DishVO> dishVO = dishMapper.getDish(dishPageQueryDTO);
		return new PageResult(dishVO.getTotal(), dishVO.getResult());
	}
	
	/**
	 * 新增菜品
	 *
	 * @param dishDTO
	 */
	@Override
	@Transactional
	public void addDish(DishDTO dishDTO) {
		// 新增菜品
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		dishMapper.addDish(dish);
		// 新增菜品对应的口味
		List<DishFlavor> flavors = dishDTO.getFlavors();
		if (flavors != null && !flavors.isEmpty()) {
			flavors.forEach(e -> e.setDishId(dish.getId()));
			dishFlavorMapper.addDishFlavor(flavors);
		}
	}
	
	@Override
	public DishVO getDishId(Long id) {
		DishVO dishVO = new DishVO();
		Dish dish = dishMapper.getDishId(id);
		BeanUtils.copyProperties(dish, dishVO);
		dishVO.setFlavors(dishFlavorMapper.getFlavorDishId(id));
		return dishVO;
	}
	
	/**
	 * 根据分类id查询
	 *
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<DishVO> getDishCategoryId(Long categoryId) {
		List<DishVO> dishVO = dishMapper.getCategoryId(categoryId);
		dishVO.forEach(e -> {
			List<DishFlavor> flavorDishId = dishFlavorMapper.getFlavorDishId(e.getId());
			e.setFlavors(flavorDishId);
		});
		return dishVO.stream().filter(e -> e.getStatus().equals(StatusConstant.ENABLE)).collect(Collectors.toList());
	}
	
	/**
	 * 起售/停售菜品
	 *
	 * @param id
	 * @param status
	 */
	@Override
	public void putDishStatus(Long id, Integer status) {
		dishMapper.putDishStatus(id, status, LocalDateTime.now(), BaseContext.getCurrentId());
	}
	
	/**
	 * 删除菜品
	 *
	 * @param ids
	 */
	@Override
	public void deleteDish(List<Long> ids) {
		// 查看要删除菜品是否在起售
		ids.forEach(e -> {
			DishVO dish = getDishId(e);
			if (dish.getStatus().equals(StatusConstant.ENABLE)) {
				throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
			}
		});
		// 查看要删除的菜品是否在套餐内
		List<Long> dishIds = setmealDishMapper.getDishIds(ids);
		if (dishIds != null && !dishIds.isEmpty()) {
			throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
		}
		// 删除菜品
		dishMapper.deleteDish(ids);
		// 删除菜品对应口味
		dishFlavorMapper.deleteFlavor(ids);
	}
	
	/**
	 * 修改菜品
	 *
	 * @param dishDTO
	 */
	@Override
	public void putDish(DishDTO dishDTO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		dishMapper.putDish(dish);
		// 删除口味数据
		dishFlavorMapper.deleteFlavor(Collections.singletonList(dish.getId()));
		// 新增菜品对应的口味
		List<DishFlavor> flavors = dishDTO.getFlavors();
		if (flavors != null && !flavors.isEmpty()) {
			flavors.forEach(e -> e.setDishId(dishDTO.getId()));
			dishFlavorMapper.addDishFlavor(flavors);
		}
	}
}
















